#!/bin/sh -x

# Oracle/prefix paths
PREFIX="${PKG_PREFIX}"
ORACLEPREFIX="/usr/lib/oracle"

# default Oracle user/group/directory, should be propagated from the Makefile
ORAUSER=${ORACLEUSER:-oracle}
ORAGROUP=${ORACLEGROUP:-dba}
ORADIR="${ORACLEPREFIX}/xe"

case $2 in
PRE-INSTALL)
#
# preinstall script extracted from the OracleXE RPM package
# and adapted for FreeBSD environment
#

# User must be root

if [ $(id -u) != "0" ]
then
    echo "You must be the root to install the software" >&2
    exit 1
fi

# ORACLE_BASE must be unset
if `env | grep -q ORACLE_BASE`
then
	/bin/sh -c "unset ORACLE_BASE"
fi

# Check for the existence of XE instance

if [ -f /etc/oratab ]
then
if `grep -q ^XE: /etc/oratab`
then
	echo "The install cannot proceed because a database instance named 'XE' appears to be
configured on the system.  Delete the instance, remove the entry corresponding 
to this instance from the oratab file (/etc/oratab), and retry the installation."
	exit 1
fi
fi

# Check for sufficient diskspace
if [ -d "$ORACLEPREFIX" ] 
then
	target="$ORACLEPREFIX"
else
	target=`dirname "$ORACLEPREFIX"`
fi
diskspace=`df -k "$target" | grep % | tr -s " " | cut -d" " -f4 | tail -1`
diskspace=`expr $diskspace / 1024`
if [ $diskspace -lt 1536 ]
then
	echo "You have insufficient diskspace in the destination directory ($ORACLEPREFIX) 
to install Oracle Database 10g Express Edition.  The installation requires at 
least 1.5 GB free on this disk."
        exit 1
fi

# Check for sufficient SWAP space
space=`sysctl -n hw.physmem`
PhyMem=`expr $space / 1048576`
swapspace=`swapinfo -m | tail -n +2 | awk '{total += $4} END {print total}'`

if [ $PhyMem -lt 240 ]
then
        echo "Oracle Database 10g Express Edition requires a minimum of 256 MB of physical 
memory (RAM).  This system has $PhyMem MB of RAM and does not meet minimum 
requirements."
        exit 1
fi

if [ $PhyMem  -gt 0 ] && [ $PhyMem -le 256 ]
then
 	required=`expr 3 \* $PhyMem`
	if [ $swapspace -lt "$required" ]
	then
		echo "This system does not meet the minimum requirements for swap space.  Based on 
the amount of physical memory available on the system, Oracle Database 10g 
Express Edition requires $required MB of swap space. This system has $swapspace MB 
of swap space.  Configure more swap space on the system and retry the installation."
		exit 1
	fi
elif [ $PhyMem -gt 256 ] && [ $PhyMem -le 512 ]
then	 
	required=`expr 2 \* $PhyMem`
	if [ $swapspace -lt "$required" ]
	then
		echo "This system does not meet the minimum requirements for swap space.  Based on 
the amount of physical memory available on the system, Oracle Database 10g 
Express Edition requires $required MB of swap space. This system has $swapspace MB 
of swap space.  Configure more swap space on the system and retry the installation."
		exit 1
	fi
elif [ $PhyMem -gt 512 ] && [ $PhyMem -le 1024 ]
then
	required=`echo 1.5 \* $PhyMem | bc`
	if test `echo $required \> 1024 | bc`
	then
		required=1024
	fi
	
	check=`echo $swapspace \< $required | bc`
	if test $check != 0
	then
		echo "This system does not meet the minimum requirements for swap space.  Based on 
the amount of physical memory available on the system, Oracle Database 10g 
Express Edition requires $required MB of swap space. This system has $swapspace MB 
of swap space.  Configure more swap space on the system and retry the installation."
		exit 1
	fi
elif [ $PhyMem -gt  1024 ] 
then
	if [ $swapspace -lt 1024 ]
	then
		echo "This system does not meet the minimum requirements for swap space.  Based on 
the amount of physical memory available on the system, Oracle Database 10g 
Express Edition requires 1024 MB of swap space. This system has $swapspace MB 
of swap space.  Configure more swap space on the system and retry the installation."
	fi
fi


# Check and Update Kernel parameters
semmsl=`sysctl -n kern.ipc.semmsl`
semmns=`sysctl -n kern.ipc.semmns`
semopm=`sysctl -n kern.ipc.semopm`
semmni=`sysctl -n kern.ipc.semmni`
shmmax=`sysctl -n kern.ipc.shmmax`
shmmni=`sysctl -n kern.ipc.shmmni`
shmall=`sysctl -n kern.ipc.shmall`
filemax=`sysctl -n kern.maxfiles`
ip_local_port_range_lb=`sysctl -n net.inet.ip.portrange.first`
ip_local_port_range_ub=`sysctl -n net.inet.ip.portrange.last`

change=no
reboot=no

changesemmsl=no
if [ $semmsl -lt 250 ]
then
        semmsl=250 
	changesemmsl=yes
	change=yes
fi

changesemmns=no
if [ $semmns -lt 32000 ]
then
        semmns=32000
	changesemmns=yes
	change=yes
fi

changesemopm=no
if [ $semopm -lt 100 ]
then
        semopm=100
	changesemopm=yes
	change=yes
fi

changesemmni=no
if [ $semmni -lt 128 ]
then
        semmni=128
	changesemmni=yes
	change=yes
fi

if [ "$change" != "no" ]
then
	echo "###########" >> /etc/sysctl.conf
	echo "# Oracle XE Recommended Values" >> /etc/sysctl.conf
fi

if [ "$changesemmsl" != "no" ]
then
	sysctl -w kern.ipc.semmsl="$semmsl"
	sysctl -e kern.ipc.semmsl >> /etc/sysctl.conf
fi

if [ "$changesemmns" != "no" ]
then
	echo kern.ipc.semmns=\"$semmns\" >>/boot/loader.conf
	reboot=yes
fi

if [ "$changesemopm" != "no" ]
then
	echo kern.ipc.semopm=\"$semopm\" >>/boot/loader.conf
	reboot=yes
fi

if [ "$changesemmni" != "no" ]
then
	echo kern.ipc.semmni=\"$semmni\" >>/boot/loader.conf
	reboot=yes
fi

changeshmmax=no	
if [ $shmmax -lt 536870912 ]
then
	shmmax=536870912
	changeshmmax=yes
fi

changeshmmni=no
if [ $shmmni -lt 4096 ]
then
	shmmni=4096
	changeshmmni=yes
fi

changeshmall=no
if [ $shmall -lt 2097152 ]
then
	 shmall=2097152
	 changeshmall=yes
fi

changefilemax=no
if [ $filemax -lt 65536 ]
then
	filemax=65536
	changefilemax=yes
fi

if [ "$changeshmmax" != "no" ]
then
	sysctl -w kern.ipc.shmmax="$shmmax"
	sysctl -e kern.ipc.shmmax >> /etc/sysctl.conf
fi

if [ "$changeshmmni" != "no" ]
then
	echo kern.ipc.shmmni=\"$shmmni\" >>/boot/loader.conf
	reboot=yes
fi

if [ "$changeshmall" != "no" ]
then
	sysctl -w kern.ipc.shmall="$shmall"
	sysctl -e kern.ipc.shmall >> /etc/sysctl.conf
fi

if [ "$changefilemax" != "no" ]
then
	sysctl -w kern.maxfiles="$filemax"
	sysctl -e kern.maxfiles >> /etc/sysctl.conf
fi

changelbport=no
if [ $ip_local_port_range_lb -lt 1024 ]
then
        ip_local_port_range_lb=1024
	changelbport=yes
fi

changeubport=no
if [ $ip_local_port_range_ub -gt 65000 ]
then
        ip_local_port_range_ub=65000
	changeubport=yes
fi

if [ "$changelbport" != "no" ]
then
	sysctl -w net.inet.ip.portrange.first="$ip_local_port_range_lb"
	sysctl -e net.inet.ip.portrange.first >> /etc/sysctl.conf
fi

if [ "$changeubport" != "no" ]
then
	sysctl -w net.inet.ip.portrange.last="$ip_local_port_range_ub"
	sysctl -e net.inet.ip.portrange.last >> /etc/sysctl.conf
fi

if [ "$change" != "no" ] || [ "$changeport" != "no" ] || [ "$changeshmmax" != "no" ] || [ "$changeshmmni" != "no" ] || [ "$changeshmall" != "no" ] || [ "$changefilemax" != "no" ]
then
	echo "########" >> /etc/sysctl.conf
fi

if [ "$reboot" != "no" ]
then
	echo "Warning: some of the IPC value changes require a system reboot in"
	echo "order to take effect."
fi

if `grep -q ^${ORAGROUP}: /etc/group`
then
	echo ""
else
	/usr/sbin/pw groupadd $ORAGROUP
fi
id $ORAUSER > /dev/null 2>&1
status=$?
if test $status -eq 0
then
        groups $ORAUSER | grep $ORAGROUP > /dev/null
	status=$?
        if test $status != 0
        then
                /usr/sbin/pw usermod $ORAUSER -G $ORAGROUP
        fi
else
        /usr/sbin/pw useradd $ORAUSER -m -g $ORAGROUP -d $ORADIR
fi

# End of pre-install script
exit 0
;; 


POST-INSTALL)
#
# postinstall script extracted from the OracleXE RPM package
# and adapted for FreeBSD environment
#
echo "Executing Post-install steps..."

/usr/sbin/chown -R $ORAUSER:$ORAGROUP $ORADIR
#XXX
/compat/linux/sbin/ldconfig >/dev/null

mkdir -p -m 0755 ${PREFIX}/share/pixmaps

# SGA & PGA algo
sgamin=146800640
pgamin=16777216
sgamax=805306368
pgamax=268435456
TM=`sysctl -n hw.physmem`
TM=`echo $TM / 1024 / 1024 | bc`
TM=`echo 0.40 \* $TM | bc | sed "s/\..*//"`
TMSP=`echo $TM-40 | bc`
sga_target=`echo 0.75 \* $TMSP | bc`
pga_target=`echo 0.25 \* $TMSP | bc `
sga=`echo $sga_target \* 1048576  | bc | sed "s/\..*//"`
pga=`echo $pga_target \* 1048576  | bc | sed "s/\..*//"`
check=`echo $sga \< $sgamin | bc`
if test $check != 0
then
        sga=$sgamin
fi

check=`echo $pga \< $pgamin | bc`
if test $check != 0
then
        pga=$pgamin
fi

if test `echo $sga + $pga \> $sgamax + $pgamax | bc`
then
	check=`echo $sga \> $sgamax | bc`
	if test $check != 0
	then
		sga=$sgamax
	fi

	check=`echo $pga \> $pgamax | bc`
	if test $check != 0
	then
		pga=$pgamax
	fi
fi

# init.ora parameters instantiation

/usr/bin/sed -i "" "s/%sga_target%/$sga/g" $ORADIR/app/oracle/product/10.2.0/server/config/scripts/init.ora
/usr/bin/sed -i "" "s/%pga_aggregate_target%/$pga/g" $ORADIR/app/oracle/product/10.2.0/server/config/scripts/init.ora
/usr/bin/sed -i "" "s/%sga_target%/$sga/g" $ORADIR/app/oracle/product/10.2.0/server/config/scripts/initXETemp.ora
/usr/bin/sed -i "" "s/%pga_aggregate_target%/$pga/g" $ORADIR/app/oracle/product/10.2.0/server/config/scripts/initXETemp.ora

/bin/chmod 6751 $ORADIR/app/oracle/product/10.2.0/server/bin/oracle
/bin/chmod 755 $ORADIR/app/oracle/product/10.2.0/server/bin/sqlplus

# Start Menu icons

if test -d  /var/lib/menu/kde/Applications/OracleXE
then
	echo ""
else
	mkdir -p /var/lib/menu/kde/Applications/OracleXE/GetHelp
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/.directory ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/.directory
	ln -s /usr/share/desktop-menu-files/oraclexe-10g.directory /var/lib/menu/kde/Applications/OracleXE/.directory
else
	ln -s /usr/share/desktop-menu-files/oraclexe-10g.directory /var/lib/menu/kde/Applications/OracleXE/.directory
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-startdb.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-startdb.desktop
	ln -s /usr/share/applications/oraclexe-startdb.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-startdb.desktop
else
	ln -s /usr/share/applications/oraclexe-startdb.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-startdb.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-stopdb.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-stopdb.desktop
	ln -s /usr/share/applications/oraclexe-stopdb.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-stopdb.desktop
else
	ln -s /usr/share/applications/oraclexe-stopdb.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-stopdb.desktop
fi


if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-dbhp.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-dbhp.desktop
	ln -s /usr/share/applications/oraclexe-dbhp.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-dbhp.desktop
else
	ln -s /usr/share/applications/oraclexe-dbhp.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-dbhp.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-getstarted.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-getstarted.desktop
	ln -s /usr/share/applications/oraclexe-getstarted.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-getstarted.desktop
else
	ln -s /usr/share/applications/oraclexe-getstarted.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-getstarted.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-runsql.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-runsql.desktop
	ln -s /usr/share/applications/oraclexe-runsql.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-runsql.desktop
else
	ln -s /usr/share/applications/oraclexe-runsql.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-runsql.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-backup.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-backup.desktop
	ln -s /usr/share/applications/oraclexe-backup.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-backup.desktop
else
	ln -s /usr/share/applications/oraclexe-backup.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-backup.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/oraclexe-restore.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/oraclexe-restore.desktop
	ln -s /usr/share/applications/oraclexe-restore.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-restore.desktop
else
	ln -s /usr/share/applications/oraclexe-restore.desktop /var/lib/menu/kde/Applications/OracleXE/oraclexe-restore.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/GetHelp/.directory ] 
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/GetHelp/.directory
	ln -s /usr/share/desktop-menu-files/oraclexe-gethelp.directory /var/lib/menu/kde/Applications/OracleXE/GetHelp/.directory
else
	ln -s /usr/share/desktop-menu-files/oraclexe-gethelp.directory /var/lib/menu/kde/Applications/OracleXE/GetHelp/.directory
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-registerforonlineforum.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-registerforonlineforum.desktop
	ln -s /usr/share/applications/oraclexe-registerforonlineforum.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-registerforonlineforum.desktop
else
	ln -s /usr/share/applications/oraclexe-registerforonlineforum.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-registerforonlineforum.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readdocumentation.desktop ]
then
	rm -fr  /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readdocumentation.desktop
	ln -s /usr/share/applications/oraclexe-readdocumentation.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readdocumentation.desktop
else
	ln -s /usr/share/applications/oraclexe-readdocumentation.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readdocumentation.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-gotoonlineforum.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-gotoonlineforum.desktop
	ln -s /usr/share/applications/oraclexe-gotoonlineforum.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-gotoonlineforum.desktop
else
	ln -s /usr/share/applications/oraclexe-gotoonlineforum.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-gotoonlineforum.desktop
fi

if [ -f /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readonlinehelp.desktop ]
then
	rm -fr /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readonlinehelp.desktop
	ln -s /usr/share/applications/oraclexe-readonlinehelp.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readonlinehelp.desktop
else
	ln -s /usr/share/applications/oraclexe-readonlinehelp.desktop /var/lib/menu/kde/Applications/OracleXE/GetHelp/oraclexe-readonlinehelp.desktop
fi

if [ -f /etc/xdg/menus/applications.menu ]
then
	cp -f /usr/share/desktop-menu-files/oraclexe-10g.directory /usr/share/desktop-directories/oraclexe-10g.directory
	cp -f /usr/share/desktop-menu-files/oraclexe-gethelp.directory /usr/share/desktop-directories/oraclexe-gethelp.directory
	cp -f /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/config/scripts/oraclexe.menu /etc/xdg/menus
	sed -i '1,/<\/Menu>/ { /<\/Menu>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/config/scripts/oraclexe-merge.menu
	}' /etc/xdg/menus/applications.menu 
elif [ -f /etc/X11/desktop-menus/applications.menu ]
then
	sed -i '1,/<\/Query>/ { /<\/Query>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/config/scripts/oraclexe-rhel3.menu
	}' /etc/X11/desktop-menus/applications.menu 
elif [ -f /etc/xdg/menus/applications-merged/kde-essential.menu ]
then
	cp -f /usr/share/desktop-menu-files/oraclexe-10g.directory /usr/share/desktop-directories/oraclexe-10g.directory
        cp -f /usr/share/desktop-menu-files/oraclexe-gethelp.directory /usr/share/desktop-directories/oraclexe-gethelp.directory
        cp -f /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/config/scripts/oraclexe.menu /etc/xdg/menus
	sed -i '1,/<\/Menu>/ { /<\/Menu>/ r /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/config/scripts/oraclexe-asianux.menu
	}' /etc/xdg/menus/applications-merged/kde-essential.menu 
fi

homedir=`echo $HOME`
if [ "$homedir" = "/root" ]
then
	homedir=`sh -c "echo ~$USER"`
fi

home=`echo $homedir | cut -d "/" -f2`

drive=`df -l \/$home | head -2 | tail -1 | awk '{print $1}'`
check=`expr "$drive" : '\(^/dev*\)'`
if [ "$check" != "" ]; 
then
	if [ "$homedir" != "/root" ]
	then
		if [ -d $homedir/.gnome-desktop ]
		then
			cp -f $PREFIX/share/applications/oraclexe-getstarteddesktop.desktop $homedir/.gnome-desktop
		fi
		if [ -d $homedir/Desktop ]
		then
			cp -f $PREFIX/applications/oraclexe-getstarteddesktop.desktop $homedir/Desktop
		fi
	fi
fi

rm -fr $ORACLEPREFIX/screenrc
rm -fr $ORACLEPREFIX/gtkrc
rm -fr $ORACLEPREFIX/emacs
rm -fr $ORACLEPREFIX/cshrc-DEFAULT_old
rm -fr $ORACLEPREFIX/cshrc-DEFAULT.06292004
rm -fr $ORACLEPREFIX/cshrc-DEFAULT
rm -fr $ORACLEPREFIX/cshrc
rm -fr $ORACLEPREFIX/bashrc-DEFAULT
rm -fr $ORACLEPREFIX/bashrc_logout
rm -fr $ORADIR/app/oracle/product/10.2.0/server/rdbms/admin/patch
/usr/bin/sed -i "" 's/\$MKDIR/\/bin\/mkdir/g' $ORADIR/app/oracle/product/10.2.0/server/network/install/sqlnet/setowner.sh
/usr/bin/sed -i "" 's/\$CHMOD/\/bin\/chmod/g' $ORADIR/app/oracle/product/10.2.0/server/network/install/sqlnet/setowner.sh
/usr/bin/sed -i "" 's/\$CHOWN/\/usr\/sbin\/chown/g' $ORADIR/app/oracle/product/10.2.0/server/network/install/sqlnet/setowner.sh
sh $ORADIR/app/oracle/product/10.2.0/server/network/install/sqlnet/setowner.sh

echo -e "You must run '/usr/local/etc/rc.d/oracle-xe configure' as the root user to 
configure the database."
echo

# End of post-install script
exit 0
;;

*)
  ;;

esac
